/*

Reverse order of blogger entries

based on: http://www.inventric.com/blog/2004/12/change-order-of-blogger-posts.html

*/

<!-- BELOW BLOGGER TAGS -->
<script type="text/javascript">

// settings
var date_header_id = 'date_header_block_';
var item_id = 'item_';
var container_id = "blogger_stack_child";

// declarations
var item_container_div = document.getElementById(container_id);
var html_items_reversed = '';
var div_tag_start = '';
var div_tag_end = '</div>';
var dh_count = 0;

// Build Date Header Array (by id setting)
var ITEM_DATE_HEADER = new Array(<Blogger>date_header_id + "<$BlogItemNumber$>", </Blogger> "");

// Build Blogger Item Array (by id setting)
var BLOGGER_ITEM = new Array(<Blogger>item_id + "<$BlogItemNumber$>", </Blogger> "");

// Move date header blocks to last item of each date (which will be first upon reversal)
for ( var itm=0; itm < BLOGGER_ITEM.length-1; itm++ )
{
	// Does item have date header?   If so, put the last date header on the item above it (unless it is the first one)
	if ( document.getElementById(BLOGGER_ITEM[itm]).innerHTML.indexOf(date_header_id) > 0 )
	{
		// Is there an item without a date header preceding this one?  If so, put hte last header on the previous item
		if ( document.getElementById(BLOGGER_ITEM[itm-1])!= null && document.getElementById(BLOGGER_ITEM[itm-1]).innerHTML.indexOf(date_header_id) < 0 )
		{
			document.getElementById(BLOGGER_ITEM[itm-1]).insertBefore(document.getElementById(ITEM_DATE_HEADER[dh_count-1]), document.getElementById(BLOGGER_ITEM[itm-1]).firstChild);
		}
		
		dh_count++;
	}
	
	// Is this the last item?  If it is, and it doesn't have a header, it gets the last header
	elseif ( (document.getElementById(BLOGGER_ITEM[itm]).innerHTML.indexOf(date_header_id) < 0) && (document.getElementById(BLOGGER_ITEM[itm+1]) == null) )
	{
		document.getElementById(BLOGGER_ITEM[itm]).insertBefore(document.getElementById(ITEM_DATE_HEADER[dh_count]), document.getElementById(BLOGGER_ITEM[itm]).firstChild);
	}
}

// Reverse Item Array
BLOGGER_ITEM.reverse();

// Collect Reversed Content
for( var itm=0; itm < BLOGGER_ITEM.length; itm++)
{
	div_tag_start = '<div id="' + BLOGGER_ITEM[itm] + '">';
	html_items_reversed = html_items_reversed + div_tag_start + document.getElementById(BLOGGER_ITEM[itm]).innerHTML + div_tag_end;
}

container_id.innerHTML = html_items_reversed;

</script>
